package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"tik-tok-mgr/cache"
	"tik-tok-mgr/model"
	"time"
)

func GoodAdd(c *gin.Context) {
	good_name := c.PostForm("good_name")
	good_price := c.PostForm("good_price")
	good_num := c.PostForm("good_num")
	if good_name == "" || good_price == "" || good_num == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := model.Good{
		GoodName:  good_name,
		GoodPrice: good_price,
		GoodNum:   good_num,
	}
	err := model.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "发布失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "发布成功",
	})
	return
}
func OrderAdd(c *gin.Context) {
	gid, _ := strconv.Atoi(c.PostForm("gid"))
	order_price := c.PostForm("order_price")
	order_time := c.PostForm("order_time")
	order_people := c.PostForm("order_people")
	if order_price == "" || order_time == "" || order_people == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := model.Order{
		Gid:         gid,
		OrderPrice:  order_price,
		OrderTime:   order_time,
		OrderPeople: order_people,
	}
	err := model.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "发布失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "发布成功",
	})
	return
}

func GoodOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size
	/*
		SELECT
		orders.id as goodOrderId,
		goods.good_name as GoodName,
		goods.good_price as GoodPrice,
		goods.good_num as GoodNum,
		orders.order_price as OrderPrice,
		orders.order_time as OrderTime,
		orders.order_people as OrderPeople,
		FROM
		orders
		LEFT JOIN goods ON goods.id = orders.gid
	*/
	var goodorder []model.GoodOrder
	redis_key := "good_order_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("orders").Select(
			"orders.id as GoodOrderId",
			"goods.good_name as GoodName",
			"goods.good_price as GoodPrice",
			"goods.good_num as GoodNum",
			"orders.order_price as OrderPrice",
			"orders.order_time as OrderTime",
			"orders.order_people as OrderPeople",
		).Joins("LEFT JOIN goods ON goods.id = orders.gid").
			Offset(offset).Limit(size).Debug().Find(&goodorder)
		jsonData, _ := json.Marshal(goodorder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &goodorder)
	}
	var total int64
	model.DB.Model(&model.Order{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "列表展示成功",
		"data":  goodorder,
		"total": total,
	})
	return
}
func GoodOrderDetail(c *gin.Context) {
	id := c.Query("id")
	/*
		SELECT
		orders.id as goodOrderId,
		goods.good_name as GoodName,
		goods.good_price as GoodPrice,
		goods.good_num as GoodNum,
		orders.order_price as OrderPrice,
		orders.order_time as OrderTime,
		orders.order_people as OrderPeople,
		FROM
		orders
		LEFT JOIN goods ON goods.id = orders.gid
	*/
	var goodorder model.GoodOrder
	redis_key := "good_order_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("orders").Select(
			"orders.id as GoodOrderId",
			"goods.good_name as GoodName",
			"goods.good_price as GoodPrice",
			"goods.good_num as GoodNum",
			"orders.order_price as OrderPrice",
			"orders.order_time as OrderTime",
			"orders.order_people as OrderPeople",
		).Joins("LEFT JOIN goods ON goods.id = orders.gid").
			Where("orders.id=?", id).Debug().First(&goodorder)
		jsonData, _ := json.Marshal(goodorder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &goodorder)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "列表展示成功",
		"data": goodorder,
	})
	return
}
